/***************************************************************************************
****************************************************************************************
* FILE		: greenProcess.c
* Description	: 
*			  
* Copyright (c) 2020 by Simlife. All Rights Reserved.
* 
* History:
* Version		Name		Date				Description
   0.1		Alen			2020/06/06		Initial Version
   
****************************************************************************************
****************************************************************************************/

#include "includes.h"

#define L1 B0011_1110
#define L2 B0011_1101
#define L3 B0011_1011
#define L4 B0011_0111
#define L5 B0010_1111

#define R1 B0101_1110
#define R2 B0101_1101
#define R3 B0101_1011
#define R4 B0101_0111
#define R5 B0100_1111

#define A1 B0111_1110
#define A2 B0111_1101
#define A3 B0111_1011
#define A4 B0111_0111
#define A5 B0110_1111

void initGreenNumberPin(void)
{
    green1PortTypeControl &= green1PinReverse;   //output
    green1PortPullupControl &= green1PinReverse; //no pull up
    GREEN1_ON();

    green2PortTypeControl &= green2PinReverse;   //output
    green2PortPullupControl &= green2PinReverse; //no pull up
    GREEN2_ON();

    green3PortTypeControl &= green3PinReverse;   //output
    green3PortPullupControl &= green3PinReverse; //no pull up
    GREEN3_ON();

    green4PortTypeControl &= green4PinReverse;   //output
    green4PortPullupControl &= green4PinReverse; //no pull up
    GREEN4_ON();

    green5PortTypeControl &= green5PinReverse;   //output
    green5PortPullupControl &= green5PinReverse; //no pull up
    GREEN5_ON();
}

void initGreenLRControlPin(void)
{
    greenLeftPortTypeControl &= greenLeftPinReverse;   //output
    greenLeftPortPullupControl &= greenLeftPinReverse; //no pull up
    GREEN_LEFT_OFF();

    greenRightPortTypeControl &= greenRightPinReverse;   //output
    greenRightPortPullupControl &= greenRightPinReverse; //no pull up
    GREEN_RIGHT_OFF();
}

void closeGreenNumberPin(void)
{
    GREEN1_ON();
    GREEN2_ON();
    GREEN3_ON();
    GREEN4_ON();
    GREEN5_ON();
}

void closeGreenLRConrotlPin(void)
{
    GREEN_LEFT_ON();
    GREEN_RIGHT_ON();
    GREEN1_ON();
    GREEN2_ON();
    GREEN3_ON();
    GREEN4_ON();
    GREEN5_ON();
}

#define greenMaxLength 58
const U8 greenMode[greenMaxLength] = {
    // 5-1 右 左
    A1, A2, A3, A4, A5, A4, A3, A2, A1,
    L1, L2, L3, L4, L5, R1, R2, R3, R4, R5,
    L1, R1, L2, R2, L3, R3, L4, R4, L5, R5,
    A5, A4, A3, A2, A1, A2, A3, A4, A5,
    R5, L5, R4, L4, R3, L3, R2, L2, R1, L1,
    A3, A4, A5, A3, A2, A1, A2, A3, A4, A5

};

void greenProcess(void) // 10ms call
{
    if (stSystem.greenMode == GREEN_MODE_OFF)
    {
        stTime.countGreenStep = 0;
        closeGreenNumberPin();
        return;
    }

    stSystem.greenFlag.byte = greenMode[stTime.countGreenStep];

    if (flagGL)
    {
        GREEN_LEFT_ON();
    }
    else
    {
        GREEN_LEFT_OFF();
    }

    if (flagGR)
    {
        GREEN_RIGHT_ON();
    }
    else
    {
        GREEN_RIGHT_OFF();
    }
    
    if (flagG1)
    {
        GREEN1_ON();
    }
    else
    {
        GREEN1_OFF();
    }

    if (flagG2)
    {
        GREEN2_ON();
    }
    else
    {
        GREEN2_OFF();
    }

    if (flagG3)
    {
        GREEN3_ON();
    }
    else
    {
        GREEN3_OFF();
    }

    if (flagG4)
    {
        GREEN4_ON();
    }
    else
    {
        GREEN4_OFF();
    }

    if (flagG5)
    {
        GREEN5_ON();
    }
    else
    {
        GREEN5_OFF();
    }


    stTime.countGreenTimes++;
    if (stTime.countGreenTimes > 99) //1s
    {
        stTime.countGreenTimes = 0;
        stTime.countGreenStep++;
        if (stTime.countGreenStep == greenMaxLength)
        {
            stTime.countGreenStep = 0;
        }
    }
}
